package com.xm.controller;


import com.xm.common.utils.JSONResult;
import com.xm.common.utils.MD5Utils;
import com.xm.pojo.Users;
import com.xm.service.UserService;

import com.xm.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@Api(value = "用户注册登录的接口",tags = {"注册和登录的controller"})
public class RegistLoginController extends BasicController{


    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册",notes = "用户注册接口")
    @PostMapping("/regist")
    public JSONResult regist(@RequestBody Users user) throws Exception{
        //用户名和密码为空
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            return JSONResult.errorMsg("用户名或密码不能为空!");
        }

        //判断用户是否存在
        boolean flag = userService.userIsExists(user.getUsername());
        if(!flag){
            user.setNickname(user.getUsername());
            user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
            user.setFansCounts(0);
            user.setReceiveLikeCounts(0);
            user.setFollowCounts(0);
            userService.saveUser(user);
        }else{
            return JSONResult.errorMsg("用户名已存在");
        }
        //保证安全，抹去密码
        user.setPassword("");

        UserVo userVo = setRedisSessionToken(user);
        return JSONResult.ok(userVo);
    }

    @ApiOperation(value = "用户登录",notes = "用户登录的接口")
    @PostMapping("/login")
    public JSONResult login(@RequestBody  Users user) throws Exception{
        //用户名和密码为空
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
                        return JSONResult.errorMsg("用户名或密码不能为空!");
        }

        user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
        user = userService.userLogin(user);
        if(user == null) return  JSONResult.errorMsg("用户名或密码错误");
        user.setPassword("");

        UserVo userVo = setRedisSessionToken(user);

        return JSONResult.ok(userVo);
    }

    @ApiOperation(value = "用户注销",notes = "用户注销的接口")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/logout")
    public JSONResult logout(String userId) throws Exception{
        redis.del(USER_REDIS_SESSION + ":" +userId);
        return JSONResult.ok();
    }

    public UserVo setRedisSessionToken(Users users){
        String userToken = UUID.randomUUID().toString();
        redis.set(USER_REDIS_SESSION + ":" + users.getId(),userToken,1000*30*60);
        UserVo userVo = new UserVo();
        userVo.setUserToken(userToken);
        BeanUtils.copyProperties(users,userVo);
        return userVo;
    }
}
